﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using Microsoft.Extensions.Logging;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class PurchaseStorageBackHandler : IRequestHandler<PurchaseStorageBackCommand, ResultsDTO<PurchaseStorageDTO>>
    {
        private readonly IBateRepository<PurchaseStorageDTO> purchase;
        private readonly ILogger<PurchaseStorageBackHandler> logger;
        public PurchaseStorageBackHandler(IBateRepository<PurchaseStorageDTO> purchase, ILogger<PurchaseStorageBackHandler> logger)
        {
            this.purchase = purchase;   
            this.logger = logger;
        }
        /// <summary>
        /// 采购入库单反填信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO<PurchaseStorageDTO>> Handle(PurchaseStorageBackCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                var conn = helper.MangoMesWrite;
                var list = new ResultsDTO<PurchaseStorageDTO>() { Code = 0 };
                var str = "SELECT a.purchaseId,a.PurchaseNo,a.PurchaseName,Supplier.SupplierName,a.PurchaseDate,a.PurchaseIndentId,a.ReceiptState,a.Remark,arrivalnotice.ArrivalNoticeId,Supplier.SupplierId FROM purchasestorage a inner join Supplier on a.supplierid=Supplier.supplierid join arrivalnotice on a.ArrivalNoticeId=arrivalnotice.ArrivalNoticeId  where PurchaseId=" + request.PurchaseId + "";
                var res = new PurchaseStorageDTO();
                list.Data = purchase.GetAll(str, res, conn).ToList();
                logger.LogInformation("成功");
                return list;
            }
            catch (Exception ex)
            {
                logger.LogError("失败" + ex);
                throw;
            }
           
        }
    }
}
